package com.liang.interview.server.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.liang.interview.api.common.PageDTO;
import com.liang.interview.api.req.InterviewHistoryReq;
import com.liang.interview.api.req.InterviewSubmitReq;
import com.liang.interview.api.vo.InterviewHistoryVO;
import com.liang.interview.api.vo.InterviewQuestionVO;
import com.liang.interview.api.vo.InterviewResultVO;
import com.liang.interview.server.entity.po.InterviewHistory;
import com.liang.interview.server.entity.po.InterviewQuestionHistory;
import com.liang.interview.server.mapper.InterviewHistoryMapper;
import com.liang.interview.server.mapper.InterviewQuestionHistoryMapper;
import com.liang.interview.server.service.InterviewHistoryService;
import com.liang.interview.server.service.InterviewQuestionHistoryService;
import com.liang.interview.server.util.LoginUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

@Service
@RequiredArgsConstructor
public class InterviewHistoryServiceImpl extends ServiceImpl<InterviewHistoryMapper, InterviewHistory> implements InterviewHistoryService {

    private final InterviewQuestionHistoryService interviewQuestionHistoryService;

    @Override
    public void logInterview(InterviewSubmitReq req, InterviewResultVO submit) {
        String keywords = req.getQuestionList().stream()
                .map(InterviewSubmitReq.Submit::getLabelName)
                .distinct()
                .collect(Collectors.joining("、"));
        InterviewHistory history = InterviewHistory.builder()
                .keywords(keywords)
                .avgScore(submit.getAvgScore())
                .tip(submit.getTips())
                .interviewUrl(req.getInterviewUrl())
                .build();
        baseMapper.insert(history);
        // 记录面试问题
        List<InterviewQuestionHistory> histories = req.getQuestionList().stream()
                .map(item -> InterviewQuestionHistory.builder()
                        .interviewId(history.getId())
                        .question(item.getSubjectName())
                        .score(item.getUserScore())
                        .keywords(item.getLabelName())
                        .answer(item.getSubjectAnswer())
                        .userAnswer(item.getUserAnswer())
                        .build())
                .collect(Collectors.toList());
        interviewQuestionHistoryService.saveBatch(histories);
    }

    @Override
    public PageDTO<InterviewHistoryVO> getHistory(InterviewHistoryReq req) {
        Page<InterviewHistory> page = req.getPageInfo().toMpPage();
        Page<InterviewHistory> p = lambdaQuery()
                .eq(InterviewHistory::getCreatedBy, LoginUtil.getLoginId())
                .eq(InterviewHistory::getIsDeleted, 0)
                .orderByDesc(InterviewHistory::getId)
                .page(page);
        return PageDTO.of(p, item -> InterviewHistoryVO.builder()
                .tip(item.getTip())
                .avgScore(item.getAvgScore())
                .keywords(item.getKeywords())
                .createdTime(item.getCreatedTime().getTime())
                .build());
    }
}
